﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Script.Services;
using ENT.GP.Core.Autenticacion;
using ENT.GP.Core.Core;
using System.Web.Script.Serialization;
namespace ENT.GP.Ws
{
    public class User : Object
    {
        public String Nombre = String.Empty;
        public String Apellido = String.Empty;
        public String StrEx   = String.Empty;
        public int Status = 0;

        public string toJSON()
        {
            string result = "{" +
                "\"Nombre\": \"" + this.Nombre + "\", " +
                "\"Apellido\": \"" + this.Apellido + "\", " +
                "\"Status\": \"" + this.Status + "\"" +
                "}";
            return result;
        }
    }

    /// <summary>
    /// Summary description for EntService
    /// </summary>
    [WebService(Namespace = "http://172.16.17.33:8080/ENT.GP.Ws/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    [System.Web.Script.Services.ScriptService]
    public class EntService : System.Web.Services.WebService
    {
        public EntService() { }
        [WebMethod(), ScriptMethod(UseHttpGet = true)]
        public string GetLogin(string sUser, string sPass)
        {
            return sUser + "_echoed!";
        }

        [WebMethod(), ScriptMethod(UseHttpGet = true,ResponseFormat = ResponseFormat.Json)]
        public string GetLogin1(string sUser, string sPass)
        {

            User cUserReturn = new User();
            try
            {

              
                Autenticacion auth;
                auth = Autenticacion.Execute(sUser, sPass);
                if (auth.Result)
                {
                    cUserReturn.Status = 1;
                    cUserReturn.Nombre = string.Format("{0}", auth.DatosUsuario.Tables[0].Rows[0][2].ToString());
                    cUserReturn.Apellido = string.Format("{0}", auth.DatosUsuario.Tables[0].Rows[0][3].ToString());

                }
               

            }
            catch (Exception ex)
            {
                cUserReturn.StrEx = ex.Message;
            }
            JavaScriptSerializer js = new JavaScriptSerializer();
            string JSON = js.Serialize(cUserReturn);
            return JSON;
        }
    }
}
